<!DOCTYPE html>
<html lang="en">
<head>
  <title>egg - Born to build better enterprise frameworks and apps</title>
  <meta charset="utf-8">
  <meta name="description" content="index.description">
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  <link rel="icon" href="/images/favicon.png" type="image/x-icon">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.css" />
<link rel="stylesheet" href="/css/index.css">

    <script>
    !function(t,e,a,r,c){t.TracertCmdCache=t.TracertCmdCache||[],t[c]=window[c]||
      {_isInit:!0,call:function(){t.TracertCmdCache.push(arguments)},
      start:function(t){this.call('start',t)}},t[c].l=new Date;
      var n=e.createElement(a),s=e.getElementsByTagName(a)[0];
      n.async=!0,n.src=r,s.parentNode.insertBefore(n,s)}
    (window,document,'script','https://tracert.alipay.com/tracert.js','Tracert');
      Tracert.start({
        plugins: [ 'BucName' ],
        spmAPos: 'a454',
        spmBPos: 'b4893',
      });
    </script>
  
</head>
<body>
  <div class="nav" >
  <header>
    <a href="/en/" class="nav-logo leftpadding" alt="egg"><img src="https://zos.alipayobjects.com/rmsportal/VTcUYAaoKqXyHJbLAPyF.svg"></a>
    <ul class="nav-item">
      <li>
        <form id="search-form">
          <input type="text" id="search-query" class="search-query st-default-search-input">
        </form>
      </li>
      <li><a href="/en/intro/" alt="Guide">Guide</a></li><li><a href="/api/" alt="API">API</a></li><li><a href="/en/tutorials/index.html" alt="Tutorials">Tutorials</a></li><li><a href="https://github.com/search?q=topic%3Aegg-plugin&type=Repositories" alt="Plugins">Plugins</a></li><li><a href="https://github.com/eggjs/egg/releases" alt="Release">Release</a></li>
      
      
        <li class="translations">
          <a class="nav-link">中文文档</a>
          <span class="arrow"></span><ul id="dropdownContent" class="dropdown-content"><li><a id="en" href="/en/tutorials/passport.html" style="color: #22ab28">English</a></li><li><a id="zh-cn" href="/zh-cn/tutorials/passport.html" >中文</a></li></ul>
        </li>
      
      <li><iframe src="https://ghbtns.com/github-btn.html?user=eggjs&repo=egg&type=star&count=true" frameborder="0" scrolling="0" width="150px" height="20px"></iframe></li>
    </ul>
    <a id="mobileTrigger" href="#" class="mobile-trigger">
      <ul>
        <li></li>
        <li></li>
        <li></li>
      </ul>
    </a>
  </header>
</div>
  <div id="container" class="container">
    <div class="page-main">
  <article class="markdown-body">
    <h1></h1>
    <h2 id="title-passport"><a class="markdown-anchor" href="#title-passport">#</a> Title: Passport</h2>
<p><strong><code>Login authentication</code></strong> is a common business scenario, including &quot;account password login&quot; and &quot;third-party unified login&quot;.</p>
<p>Among them, we often use the latter, such as Google, GitHub, QQ unified login, which are based on <a href="https://oauth.net/2/" target="_blank" rel="noopener">OAuth</a> specification.</p>
<p><a href="http://www.passportjs.org/" target="_blank" rel="noopener">Passport</a> is a highly scalable authentication middleware that supports the <code>Strategy</code> of <code>Github</code> ,<code>Twitter</code>,<code>Facebook</code>, and other well-known service vendors. It also supports login authorization verification via account passwords.</p>
<p>Egg provides an <a href="https://github.com/eggjs/egg-passport" target="_blank" rel="noopener">egg-passport</a> plugin which encapsulates general logic such as callback processing after initialization and the success of authentication so that the developers can use Passport with just a few API calls.</p>
<p>The execution sequence of <a href="http://www.passportjs.org/" target="_blank" rel="noopener">Passport</a> is as follows:</p>
<ul>
<li>User accesses page</li>
<li>Check Session</li>
<li>Intercept and jump to authentication login page</li>
<li>Strategy Authentication</li>
<li>Check and store user information</li>
<li>Serialize user information to Session</li>
<li>Jump to the specified page</li>
</ul>
<h2 id="using-egg-passport"><a class="markdown-anchor" href="#using-egg-passport">#</a> Using egg-passport</h2>
<p>Below, we will use GitHub login as an example to demonstrate how to use it.</p>
<h3 id="installation"><a class="markdown-anchor" href="#installation">#</a> Installation</h3>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">$ npm i --save egg-passport</span><br><span class="line">$ npm i --save egg-passport-github</span><br></pre></td></tr></table></figure>
<p>For more plugins, see <a href="https://github.com/topics/egg-passport" target="_blank" rel="noopener">GitHub Topic - egg-passport</a> .</p>
<h3 id="configuration"><a class="markdown-anchor" href="#configuration">#</a> Configuration</h3>
<p><strong>Enabling the plugin:</strong></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// config/plugin.js</span></span><br><span class="line"><span class="built_in">module</span>.exports.passport = &#123;</span><br><span class="line">  enable: <span class="literal">true</span>,</span><br><span class="line">  package: <span class="string">'egg-passport'</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports.passportGithub = &#123;</span><br><span class="line">  enable: <span class="literal">true</span>,</span><br><span class="line">  package: <span class="string">'egg-passport-github'</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p><strong>Configuration:</strong></p>
<p>Note: The <a href="https://github.com/eggjs/egg-passport" target="_blank" rel="noopener">egg-passport</a> standardizes the configuration fields, which are unified as <code>key</code> and <code>secret</code>.</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// config/default.js</span></span><br><span class="line">config.passportGithub = &#123;</span><br><span class="line">  key: <span class="string">'your_clientID'</span>,</span><br><span class="line">  secret: <span class="string">'your_clientSecret'</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p><strong>note:</strong></p>
<ul>
<li>Create a <a href="https://github.com/settings/applications/new" target="_blank" rel="noopener">GitHub OAuth Apps</a> to get the <code>clientID</code> and <code>clientSecret</code> information.</li>
<li>Specify a <code>callbackURL</code>, such as <code>http://127.0.0.1:7001/passport/github/callback</code>
  - You need to update to the corresponding domain name when deploying online
  - The path is configured via <code>options.callbackURL</code>, which defaults to <code>/passport/${strategy}/callback</code></li>
</ul>
<h3 id="mounting-routes"><a class="markdown-anchor" href="#mounting-routes">#</a> Mounting Routes</h3>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// app/router.js</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> &#123; router, controller &#125; = app;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// Mount the authentication route</span></span><br><span class="line">  app.passport.mount(<span class="string">'github'</span>);</span><br><span class="line"></span><br><span class="line">  <span class="comment">// The mount above is syntactic sugar, which is equivalent to</span></span><br><span class="line">  <span class="comment">// const github = app.passport.authenticate('github', &#123;&#125;);</span></span><br><span class="line">  <span class="comment">// router.get('/passport/github', github);</span></span><br><span class="line">  <span class="comment">// router.get('/passport/github/callback', github);</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="user-information-processing"><a class="markdown-anchor" href="#user-information-processing">#</a> User Information Processing</h3>
<p>Then we also need:</p>
<ul>
<li>When signing in for the first time, you generally need to put user information into the repository and record the Session.</li>
<li>In the second login, the user information obtained from OAuth or Session, and the database is read to get the complete user information.</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// app.js</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">  app.passport.verify(<span class="keyword">async</span> (ctx, user) =&gt; &#123;</span><br><span class="line">    <span class="comment">// Check user</span></span><br><span class="line">    assert(user.provider, <span class="string">'user.provider should exists'</span>);</span><br><span class="line">    assert(user.id, <span class="string">'user.id should exists'</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Find user information from the database</span></span><br><span class="line">    <span class="comment">//</span></span><br><span class="line">    <span class="comment">// Authorization Table</span></span><br><span class="line">    <span class="comment">// column   | desc</span></span><br><span class="line">    <span class="comment">// ---      | --</span></span><br><span class="line">    <span class="comment">// provider | provider name, like github, twitter, facebook, weibo and so on</span></span><br><span class="line">    <span class="comment">// uid      | provider unique id</span></span><br><span class="line">    <span class="comment">// user_id  | current application user id</span></span><br><span class="line">    <span class="keyword">const</span> auth = <span class="keyword">await</span> ctx.model.Authorization.findOne(&#123;</span><br><span class="line">      uid: user.id,</span><br><span class="line">      provider: user.provider,</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="keyword">const</span> existsUser = <span class="keyword">await</span> ctx.model.User.findOne(&#123; <span class="attr">id</span>: auth.user_id &#125;);</span><br><span class="line">    <span class="keyword">if</span> (existsUser) &#123;</span><br><span class="line">      <span class="keyword">return</span> existsUser;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// Call service to register a new user</span></span><br><span class="line">    <span class="keyword">const</span> newUser = <span class="keyword">await</span> ctx.service.user.register(user);</span><br><span class="line">    <span class="keyword">return</span> newUser;</span><br><span class="line">  &#125;);</span><br><span class="line"></span><br><span class="line">  <span class="comment">// Serialize and store the user information into session. Generally, only a few fields need to be streamlined/saved.</span></span><br><span class="line">  app.passport.serializeUser(<span class="keyword">async</span> (ctx, user) =&gt; &#123;</span><br><span class="line">  <span class="comment">// process user</span></span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">  <span class="comment">// return user;</span></span><br><span class="line">  &#125;);</span><br><span class="line"></span><br><span class="line">  <span class="comment">// Deserialize the user information from the session, check the database to get the complete information</span></span><br><span class="line">  app.passport.deserializeUser(<span class="keyword">async</span> (ctx, user) =&gt; &#123;</span><br><span class="line">  <span class="comment">// process user</span></span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">  <span class="comment">// return user;</span></span><br><span class="line">  &#125;);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>At this point, we have completed all the configurations. For a complete example, see: <a href="https://github.com/eggjs/examples/tree/master/passport" target="_blank" rel="noopener">eggjs/examples/passport</a></p>
<h3 id="api"><a class="markdown-anchor" href="#api">#</a> API</h3>
<p><a href="https://github.com/eggjs/egg-passport" target="_blank" rel="noopener">egg-passport</a> provides the following extensions:</p>
<ul>
<li><code>ctx.user</code> - Get current logged in user information</li>
<li><code>ctx.isAuthenticated()</code> - Check if the request is authorized</li>
<li><code>ctx.login(user, [options])</code> - Start a login session for the user</li>
<li><code>ctx.logout()</code> - Exit and clear user information from session</li>
<li><code>ctx.session.returnTo=</code> - Set redirect address after authentication page success</li>
</ul>
<p>The API also be provided for:</p>
<ul>
<li><code>app.passport.verify(async (ctx, user) =&gt; {})</code> - Check user</li>
<li><code>app.passport.serializeUser(async (ctx, user) =&gt; {})</code> - Serialize user information into session</li>
<li><code>app.passport.deserializeUser(async (ctx, user) =&gt; {})</code> - Deserialize user information from the session</li>
<li><code>app.passport.authenticate(strategy, options)</code> - Generate the specified authentication middleware
  - <code>options.successRedirect</code> - specifies the redirect address after successful authentication
  - <code>options.loginURL</code> - jump login address, defaults to <code>/passport/${strategy}</code>
  - <code>options.callbackURL</code> - callback address after authorization, defaults to <code>/passport/${strategy}/callback</code></li>
<li><code>app.passport.mount(strategy, options)</code> - Syntactic sugar for developers to configure routing</li>
</ul>
<h2 id="using-passport-ecosystem"><a class="markdown-anchor" href="#using-passport-ecosystem">#</a> Using Passport Ecosystem</h2>
<p><a href="http://www.passportjs.org/" target="_blank" rel="noopener">Passport</a> has many middleware and it is impossible to have the second encapsulation.
Next, let's look at how to use Passport middleware directly in the framework.
We will use <a href="https://github.com/jaredhanson/passport-local" target="_blank" rel="noopener">passport-local</a> for &quot;account password login&quot; as an example:</p>
<h3 id="installation-2"><a class="markdown-anchor" href="#installation-2">#</a> Installation</h3>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">$ npm i --save passport-local</span><br></pre></td></tr></table></figure>
<h3 id="configuration-2"><a class="markdown-anchor" href="#configuration-2">#</a> Configuration</h3>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// app.js</span></span><br><span class="line"><span class="keyword">const</span> LocalStrategy = <span class="built_in">require</span>(<span class="string">'passport-local'</span>).Strategy;</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="comment">// Mount strategy</span></span><br><span class="line">  app.passport.use(<span class="keyword">new</span> LocalStrategy(&#123;</span><br><span class="line">    passReqToCallback: <span class="literal">true</span>,</span><br><span class="line">  &#125;, (req, username, password, done) =&gt; &#123;</span><br><span class="line">    <span class="comment">// format user</span></span><br><span class="line">    <span class="keyword">const</span> user = &#123;</span><br><span class="line">      provider: <span class="string">'local'</span>,</span><br><span class="line">      username,</span><br><span class="line">      password,</span><br><span class="line">    &#125;;</span><br><span class="line">    debug(<span class="string">'%s %s get user: %j'</span>, req.method, req.url, user);</span><br><span class="line">    app.passport.doVerify(req, user, done);</span><br><span class="line">  &#125;));</span><br><span class="line"></span><br><span class="line">  <span class="comment">// Process user information</span></span><br><span class="line">  app.passport.verify(<span class="keyword">async</span> (ctx, user) =&gt; &#123;&#125;);</span><br><span class="line">  app.passport.serializeUser(<span class="keyword">async</span> (ctx, user) =&gt; &#123;&#125;);</span><br><span class="line">  app.passport.deserializeUser(<span class="keyword">async</span> (ctx, user) =&gt; &#123;&#125;);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h3 id="mounting-routes-2"><a class="markdown-anchor" href="#mounting-routes-2">#</a> Mounting Routes</h3>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// app/router.js</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> &#123; router, controller &#125; = app;</span><br><span class="line">  router.get(<span class="string">'/'</span>, controller.home.index);</span><br><span class="line"></span><br><span class="line">  <span class="comment">// Callback page after successful authentication</span></span><br><span class="line">  router.get(<span class="string">'/authCallback'</span>, controller.home.authCallback);</span><br><span class="line"></span><br><span class="line">  <span class="comment">// Render login page, user inputs account password</span></span><br><span class="line">  router.get(<span class="string">'/login'</span>, controller.home.login);</span><br><span class="line">  <span class="comment">// Login verification</span></span><br><span class="line">  router.post(<span class="string">'/login'</span>, app.passport.authenticate(<span class="string">'local'</span>, &#123; <span class="attr">successRedirect</span>: <span class="string">'/authCallback'</span> &#125;));</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h2 id="how-to-develop-an-egg-passport-plugin"><a class="markdown-anchor" href="#how-to-develop-an-egg-passport-plugin">#</a> How to develop an egg-passport plugin</h2>
<p>In the previous section, we learned how to use a Passport middleware in the framework. We can further encapsulate it as a plugin and give back to the community.</p>
<p><strong>initialization:</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">$ egg-init --<span class="built_in">type</span>=plugin egg-passport-local</span><br></pre></td></tr></table></figure>
<p><strong>Configure dependencies in <code>package.json</code>:</strong></p>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">"name"</span>: <span class="string">"egg-passport-local"</span>,</span><br><span class="line">  <span class="attr">"version"</span>: <span class="string">"1.0.0"</span>,</span><br><span class="line">  <span class="attr">"eggPlugin"</span>: &#123;</span><br><span class="line">    <span class="attr">"name"</span>: <span class="string">"passportLocal"</span>,</span><br><span class="line">    <span class="attr">"dependencies"</span>: [<span class="string">"passport"</span>]</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">"dependencies"</span>: &#123;</span><br><span class="line">    <span class="attr">"passport-local"</span>: <span class="string">"^1.0.0"</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>Configuration:</strong></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// &#123;plugin_root&#125;/config/config.default.js</span></span><br><span class="line"><span class="comment">// https://github.com/jaredhanson/passport-local</span></span><br><span class="line">exports.passportLocal = &#123;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>Note: <a href="https://github.com/eggjs/egg-passport" target="_blank" rel="noopener">egg-passport</a> standardizes the configuration fields, which are unified as <code>key</code> and <code>secret</code>, so if the corresponding Passport middleware attribute names are inconsistent, the developer should do the conversion.</p>
<p><strong>Register the passport middleware:</strong></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// &#123;plugin_root&#125;/app.js</span></span><br><span class="line"><span class="keyword">const</span> LocalStrategy = <span class="built_in">require</span>(<span class="string">'passport-local'</span>).Strategy;</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> config = app.config.passportLocal;</span><br><span class="line">  config.passReqToCallback = <span class="literal">true</span>;</span><br><span class="line"></span><br><span class="line">  app.passport.use(<span class="keyword">new</span> LocalStrategy(config, (req, username, password, done) =&gt; &#123;</span><br><span class="line">    <span class="comment">// Cleans up the data returned by the Passport plugin and returns the User object</span></span><br><span class="line">    <span class="keyword">const</span> user = &#123;</span><br><span class="line">      provider: <span class="string">'local'</span>,</span><br><span class="line">      username,</span><br><span class="line">      password,</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="comment">// This does not process application-level logic and passes it to app.passport.verify for unified processing.</span></span><br><span class="line">    app.passport.doVerify(req, user, done);</span><br><span class="line">  &#125;));</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

  </article>
  <aside id="mobileAside" class="toc">
  <div class="mobile-menu">
    <ul>
      <li><a href="/en/intro/" alt="Guide">Guide</a></li><li><a href="/api/" alt="API">API</a></li><li><a href="/en/tutorials/index.html" alt="Tutorials">Tutorials</a></li><li><a href="https://github.com/search?q=topic%3Aegg-plugin&type=Repositories" alt="Plugins">Plugins</a></li><li><a href="https://github.com/eggjs/egg/releases" alt="Release">Release</a></li>
      
      
        <li class="translations">
          <a class="nav-link">中文文档</a>
          <span class="arrow"></span><ul id="dropdownContent" class="dropdown-content"><li><a id="en" href="/en/tutorials/passport.html" style="color: #22ab28">English</a></li><li><a id="zh-cn" href="/zh-cn/tutorials/passport.html" >中文</a></li></ul>
        </li>
      
    </ul>
  </div>
  <dl><dt id="title-Intro" style="cursor: pointer;" class="aside-title">Guide<a id="collapse-icon-Intro" class="icon opend"></a></dt><dd id=panel-Intro><ul><li><a href="/en/intro/index.html" class="menu-link">What is Egg?</a></li><li><a href="/en/intro/egg-and-koa.html" class="menu-link">Egg and Koa</a></li><li><a href="/en/intro/quickstart.html" class="menu-link">Quick Start</a></li><li><a href="/en/tutorials/progressive.html" class="menu-link">Progressive</a></li><li><a href="/en/migration.html" class="menu-link">Migration to 2.x</a></li></ul></dd><dt id="title-Basics" style="cursor: pointer;" class="aside-title">Basis Function<a id="collapse-icon-Basics" class="icon opend"></a></dt><dd id=panel-Basics><ul><li><a href="/en/basics/structure.html" class="menu-link">Directory Structure</a></li><li><a href="/en/basics/objects.html" class="menu-link">Built-in Objects</a></li><li><a href="/en/basics/env.html" class="menu-link">Environment</a></li><li><a href="/en/basics/config.html" class="menu-link">Configuration</a></li><li><a href="/en/basics/middleware.html" class="menu-link">Middleware</a></li><li><a href="/en/basics/router.html" class="menu-link">Router</a></li><li><a href="/en/basics/controller.html" class="menu-link">Controller</a></li><li><a href="/en/basics/service.html" class="menu-link">Service</a></li><li><a href="/en/basics/plugin.html" class="menu-link">Plugin</a></li><li><a href="/en/basics/schedule.html" class="menu-link">Schedule</a></li><li><a href="/en/basics/extend.html" class="menu-link">Extend</a></li><li><a href="/en/basics/app-start.html" class="menu-link">Custom Init</a></li></ul></dd><dt id="title-Core" style="cursor: pointer;" class="aside-title">Core<a id="collapse-icon-Core" class="icon opend"></a></dt><dd id=panel-Core><ul><li><a href="/en/core/development.html" class="menu-link">Development</a></li><li><a href="/en/core/unittest.html" class="menu-link">Unit Testing</a></li><li><a href="/en/core/deployment.html" class="menu-link">Deployment</a></li><li><a href="/en/core/logger.html" class="menu-link">Logger</a></li><li><a href="/en/core/httpclient.html" class="menu-link">HttpClient</a></li><li><a href="/en/core/cookie-and-session.html" class="menu-link">Cookie and Session</a></li><li><a href="/en/core/cluster-and-ipc.html" class="menu-link">Cluster and IPC</a></li><li><a href="/en/core/view.html" class="menu-link">View</a></li><li><a href="/en/core/error-handling.html" class="menu-link">Error Handling</a></li><li><a href="/en/core/security.html" class="menu-link">Security</a></li><li><a href="/en/core/i18n.html" class="menu-link">i18n</a></li></ul></dd><dt id="title-Tutorials" style="cursor: pointer;" class="aside-title">Tutorials<a id="collapse-icon-Tutorials" class="icon opend"></a></dt><dd id=panel-Tutorials><ul><li><a href="/en/tutorials/mysql.html" class="menu-link">MySQL</a></li><li><a href="/en/tutorials/restful.html" class="menu-link">RESTful API</a></li><li><a href="/en/tutorials/passport.html" class="menu-link">Passport</a></li><li><a href="/en/tutorials/socketio.html" class="menu-link">Socket.IO</a></li><li><a href="/en/tutorials/assets.html" class="menu-link">Assets</a></li><li><a href="/en/tutorials/typescript.html" class="menu-link">TypeScript</a></li></ul></dd><dt id="title-Advanced" style="cursor: pointer;" class="aside-title">Advanced<a id="collapse-icon-Advanced" class="icon opend"></a></dt><dd id=panel-Advanced><ul><li><a href="/en/advanced/loader.html" class="menu-link">Loader</a></li><li><a href="/en/advanced/plugin.html" class="menu-link">Plugin Development</a></li><li><a href="/en/advanced/framework.html" class="menu-link">Framework</a></li><li><a href="/en/advanced/cluster-client.html" class="menu-link">Cluster Enhancement</a></li><li><a href="/en/advanced/view-plugin.html" class="menu-link">View Plugin</a></li><li><a href="/en/style-guide.html" class="menu-link">Style Guide</a></li></ul></dd><dt id="title-Community" style="cursor: pointer;" class="aside-title">Community<a id="collapse-icon-Community" class="icon opend"></a></dt><dd id=panel-Community><ul><li><a href="/en/plugins/" class="menu-link">Plugin List</a></li><li><a href="/en/contributing.html" class="menu-link">Contributing</a></li><li><a href="/en/resource.html" class="menu-link">Resource</a></li><li><a href="/en/faq.html" class="menu-link">FAQ</a></li></ul></dd></dl>
</aside>
<script>
var mobileTrigger = document.getElementById('mobileTrigger');
var mobileAside = document.getElementById('mobileAside');

var expandMenu = function(title) {
  // handle icon
  const collapseIcon = document.getElementById('collapse-icon-' + title);
  if (collapseIcon) {
    collapseIcon.className = 'icon opend';
  }
  // handle panelEle
  const panelEle = document.getElementById('panel-' + title);
  if (panelEle) {
    panelEle.className = '';
  }
}

var collapseMenu = function(title) {
  // handle icon
  const collapseIcon = document.getElementById('collapse-icon-' + title);
  if (collapseIcon) {
    collapseIcon.className = 'icon closed';
  }
  // handle panelEle
  const panelEle = document.getElementById('panel-' + title);
  if (panelEle) {
    panelEle.className = 'aside-panel-hidden';
  }
}

mobileAside.onclick = function(e) {
  const targetId = e.target.id;
  if (targetId && (targetId.indexOf('title-') > -1 || targetId.indexOf('collapse-icon-') > -1)) {
    const title = targetId.replace('title-', '').replace('collapse-icon-', '');
    try { 
      // the the browser may have no localStroage or JSON.parse may throw exception.
      const menuInfo = JSON.parse(window.localStorage.getItem('menuInfo'));
        
      // current menu status
      const curClosed = menuInfo[title] ? menuInfo[title].closed : false; // default false

      // change UI
      curClosed ? expandMenu(title) : collapseMenu(title);

      // save menuInfo to localStorage
      menuInfo[title] = { closed: !curClosed } // opposite
      window.localStorage.setItem('menuInfo', JSON.stringify(menuInfo));
    } catch (e) {}
  }
};

mobileTrigger.onclick = function(e) {
  e.preventDefault();
  if (mobileAside.className.indexOf('mobile-show') === -1) {
    mobileAside.className += ' mobile-show';
  } else {
    mobileAside.className = 'toc';
  }
};

(function() {
  // save data to localStorage because the page will refresh when user change the url.
  let menuInfo;
  try { 
    // the the browser may have no localStroage or JSON.parse may throw exception.
    menuInfo = JSON.parse(window.localStorage.getItem('menuInfo'));
    if (!menuInfo) {
      menuInfo = {};
      window.localStorage.setItem('menuInfo', JSON.stringify(menuInfo));
    }
  } catch (e) {
    menuInfo = {}; // default {}
  }

  for (const title in menuInfo) {
    if (menuInfo[title] && menuInfo[title].closed) { // menu in closed status.
      collapseMenu(title);
    } else {
      expandMenu(title);
    }
  }

  // highlight menu
  const pathname = window.location.pathname;
  const selector = `a[href="${pathname}"].menu-link,a[href="${pathname}index.html"].menu-link`;
  const menuItem = mobileAside.querySelector(selector);
  if (menuItem) { menuItem.className += ' highlight'; }
})();
</script>

</div>

  </div>
</body>
<script src="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.js"></script>
<script>
docsearch({
  apiKey: '1561de31a86f79507ea00cdb54ce647c',
  indexName: 'eggjs',
  inputSelector: '#search-query',
});
</script>
<div class="cnzz">
<script src="https://s11.cnzz.com/z_stat.php?id=1261142226&web_id=1261142226" language="JavaScript"></script>
</div>

</html>
